package modules.webui.control
{
	import flash.events.EventDispatcher;
	
	import modules.webui.events.WebUIEvent;
	import modules.webui.model.LinkVO;
	
	import mx.core.FlexGlobals;

	public class LinkControls extends EventDispatcher
	{
		public function LinkControls()
		{
		}
		
		public function handleGeneralLink(link: Object): void
		{
			var l: LinkVO = createLinkVO(link);
			cancelReply();
		}
		
		/** 
		 * Reply to link like "#1213/32" from javascript
		 */
		public function replyTo(link: Object, mid: *, rid: *): void
		{
			var a: String = "aa"; 
			trace("Reply to: " + link );
			
			//var l: LinkVO = createLinkVO(link);
			//var replyIDRegex: RegExp = /xmpp:juick@juick\.com\?message;body=%23(\d+)+/;
			//var replyId: String = l.href.match(replyIDRegex)[1];
			//createReply(l, replyId);			
			
			var replyId: String = "";
			if (rid != undefined)
			{
				replyId = '/' + rid; 
			}
			
			var webUIEvent: WebUIEvent = new WebUIEvent(WebUIEvent.REPLY_BY_ID);
			webUIEvent.replyId = mid + replyId;
			webUIEvent.eventX = 100;
			webUIEvent.eventY = 100;
			
			var app: JUI = FlexGlobals.topLevelApplication as JUI; 
			
			(FlexGlobals.topLevelApplication as JUI).dispatchEvent(webUIEvent);			
		}
		
		/** 
		 * Parses links like: xmpp:juick@juick.com?message;body=%23203059+\
		 */
		public function handleXMPPLink(link: Object): void
		{
			var l: LinkVO = createLinkVO(link);
			var replyIDRegex: RegExp = /xmpp:juick@juick\.com\?message;body=%23(\d+)+/;
			var replyId: String = l.href.match(replyIDRegex)[1];
			createReply(l, replyId);
		}
		
		/**
		 * Parses links like: // href=javascript:reply(203059)
		 */
		public function handleJavascriptLink(link: Object): void
		{
			var l: LinkVO = createLinkVO(link);
			var replyIDRegex: RegExp = /javascript:reply\((\d+)\)/;
			var replyId: String = l.href.match(replyIDRegex)[1];
			createReply(l, replyId);
		}
		
		private function cancelReply(): void
		{
			var cancelEvent: WebUIEvent = new WebUIEvent(WebUIEvent.CANCEL_REPLY);
			(FlexGlobals.topLevelApplication as JUI).dispatchEvent(cancelEvent);
		}
			
		private function createReply(l: LinkVO, replyId: String): void
		{
			var linkX: Number = parseInt(l.offsetLeft);
			var linkY: Number = parseInt(l.offsetTop);
			var linkW: Number = parseInt(l.offsetWidth);
			var linkH: Number = parseInt(l.offsetHeight);
			
			trace("Javascript link properties: ");
			trace("\t Reply id = " + replyId );
			trace("\t X = " + linkX );
			trace("\t Y = " + linkY );
			
			var webUIEvent: WebUIEvent = new WebUIEvent(WebUIEvent.REPLY_BY_ID);
			webUIEvent.replyId = replyId;
			//webUIEvent.eventX = linkX;
			//webUIEvent.eventY = linkY + linkH + 5;
			
			var app: JUI = FlexGlobals.topLevelApplication as JUI; 
			
			webUIEvent.eventX = linkX; //app.mouseX;
			webUIEvent.eventY = app.mouseY + 10;
			
			(FlexGlobals.topLevelApplication as JUI).dispatchEvent(webUIEvent);
		}

		public function traceLink(link: Object, verbose: Boolean = false): void
		{
			var l: LinkVO = createLinkVO(link);
			
			if(verbose)
			{
				trace("Link properties (all): " + l);	
			}
			else
			{
				trace("Link text: " + l.text + ", protocol: " + l.protocol);
			}
		}
		
		private function createLinkVO(link: Object): LinkVO
		{
			var linkVO: LinkVO = new LinkVO();
			linkVO.text=link.text;
			linkVO.href=link.href;
			linkVO.title=link.title;
			linkVO.protocol=link.protocol;
			linkVO.search=link.search;
			linkVO.innerHTML=link.innerHTML;
			linkVO.outerHTML=link.outerHTML;
			linkVO.offsetTop=link.offsetTop;
			linkVO.offsetLeft=link.offsetLeft;
			linkVO.offsetWidth=link.offsetWidth;
			linkVO.offsetHeight=link.offsetHeight;
			
			return linkVO;
		}
	}
}